<?php
// 注册页面
header("content-type:text/html;charset=utf-8");
// 判断是否为POST请求
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // 接收传值
    $data = file_get_contents("php://input"); // 解析JSON数据
    $data = json_decode($data, true);
    $uid = $data['u_uid'];
    $pwd = $data['u_pwd'];
    $phone = $data['u_phone'];
} else {
    $response = [
        'code' => 400,
        'msg' => '请求方式错误',
    ];
    echo json_encode($response);
    exit();
}

//链接数据库
include_once("conn.php");

//查询语句
$sql_select = "SELECT * FROM tb_user WHERE u_id = '" . $uid . "'";
// 执行查询
$result = mysqli_query($conn_DB, $sql_select);
if(!$result) {
    $response = [
        'code' => -2, //数据库查询失败
    ];
    echo json_encode($response);
    die();
}
$rows = mysqli_num_rows($result);
// 判断查询返回记录是否为空
//如果不为空则说明账号被使用
if ($rows != 0) {
    $response = [
        'code' => 0, //账号已被使用
    ];
} else {
    //更改时区为中国上海
    date_default_timezone_set('Asia/Shanghai');
    //获取当前时间
    $time = date('Y-m-d H:i:s', time());
    //插入语句
    $sql_insert = "INSERT INTO tb_user (u_id, u_password, u_phone, u_name, u_register) 
        VALUES ('" . $uid . "', '" . $pwd . "', '" . $phone . "', '用户" . $uid . "', '" . $time ."')";
    // 执行插入
    $result = mysqli_query($conn_DB, $sql_insert);
    if ($result) {
        $response =[
                'code' => 1, //注册成功
            ];
    } else {
        $response =[
                'code' => -1, //注册失败
            ];
    }
}
// 返回json数据
echo json_encode($response);

// $connect_DB->close();